/*
The AWS environment uses a Jenkins declarative pipeline and executes build stages using 
the Staging Cluster via a Kubernetes agent defined below.

The yaml defined under the Kubernetes agent creates a build pod with multiple containers
that can be used by any of the build stages. For this build we are using:
 
 - gradle: A custom docker image with gradle 4.10 pre-installed in order to run the build script
 - docker: An image with docker used to build the application image
 - dind-daemon: The docker engine used by the docker container. We use dind to avoid using the Kubernetes docker daemon
 - k8-deploy: A custom image with kubectl that pulls credentails at runtime to deploy to the cluster. 
 The container will look for kubernetes manifests in the ./kubernetes/ dir. It will also support 
 variable substitution in the yaml files following the $ENV_VAR pattern

*/

pipeline {
  agent {
    kubernetes {
      label 'var-resources-image-build'
      defaultContainer 'gradle'
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    image-build: var-resources-v4
spec:
  containers:
  - name: gradle
    image: utility.apps.DOMAIN.EX/base-images/gradle:4.10
    securityContext:
      privileged: true
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    command:
    - cat
    tty: true
  - name: docker
    image: docker:1.12.6
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    command:
    - cat
    tty: true
  - name: dind-daemon
    image: utility.apps.DOMAIN.EX/base-images/map-dind-image:1.0.1
    securityContext:
      privileged: true
    ports:
    - containerPort: 2375
    volumeMounts:
    - name: docker-graph-storage
      mountPath: /var/lib/docker
    tty: true
  - name: k8-deploy
    image: utility.apps.DOMAIN.EX/base-images/k8-deploy:1.0.4
    command:
    - cat
    tty: true
  imagePullSecrets:
  - name: map-dtr
  volumes:
  - name: docker-graph-storage
    emptyDir: {}
"""
    }
  }

  environment { 
      SERVICE_VERSION = '4.6.0'
      IMAGE = '/var/var-resources'
      DTR_URL = 'utility.apps.DOMAIN.EX'
      DTR_PREFIX = 'utility.apps.DOMAIN.EX'
      DTR_ORG = '/base-images'
  }


  stages {

    stage('App Build') {
      steps {
        withCredentials([string(credentialsId: 'VA_NEXUS_PWD', variable: 'VA_NEXUS_PWD'), string(credentialsId: 'VA_NEXUS_USER', variable: 'VA_NEXUS_USER'), string(credentialsId: 'MAP_DTR_PWD', variable: 'MAP_DTR_PWD'), string(credentialsId: 'MAP_DTR_USER', variable: 'MAP_DTR_USER')]) {
            container('gradle') {
              sh './build.sh'
            }
        }
      }
    }

    stage('Image Build and Push') {
      steps {
        withCredentials([string(credentialsId: 'MAP_DTR_PWD', variable: 'MAP_DTR_PWD'), string(credentialsId: 'MAP_DTR_USER', variable: 'MAP_DTR_USER')]) {
            container('docker') {
              sh 'docker login -u $MAP_DTR_USER -p $MAP_DTR_PWD utility.apps.DOMAIN.EX'
              sh 'cd dist/ && docker build -t $DTR_URL$IMAGE:$SERVICE_VERSION .'
              sh 'docker push $DTR_URL$IMAGE:$SERVICE_VERSION'
            }
        }
      }
    }

    stage('Deploy to Staging'){

        environment {
            K8_API_ADDR = 'https://internal-staging-kube-api-336601082.us-gov-west-1.elb.amazonaws.com:6443'
            VAMF_ENVIRONMENT = 'map-staging'
            VAULT_ADDR = 'http://ip-172-16-2-10.us-gov-west-1.compute.internal:8200'
            CONSUL_ADDR = 'ip-172-16-2-11.us-gov-west-1.compute.internal:8500'
            SECRET_PATH = 'secret/map-staging/deploy'
            CONSUL_PATH = 'appconfig/map-staging/deploy'
            NAMESPACE = 'default'
        }

        steps {
            withCredentials([string(credentialsId: 'CONSUL_MASTER_TOKEN', variable: 'CONSUL_TOKEN')]) {
                container('k8-deploy') {
                    sh 'echo "Deploying $DTR_URL$IMAGE:$VERSION"'
                    sh '/entrypoint.sh'
                }
            }
        }
    }

  }

  post {
    always {
      cleanWs() /* clean up our workspace */
    }
  }
}TTTTTT